package com.hopu.blb_erp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.blb_erp.entity.TIomWarehouse;
import com.hopu.blb_erp.entity.TMenu;
import com.hopu.blb_erp.service.TIomWarehouseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hopu.blb_erp.entity.TCargo;
import com.hopu.blb_erp.entity.TIomWarehouse;
import com.hopu.blb_erp.entity.TWarehouse;
import com.hopu.blb_erp.service.CargoService;
import com.hopu.blb_erp.service.TIomWarehouseService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.hopu.blb_erp.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Api
@EnableSwagger2
@RestController
public class TIomWarehouseController {
    @Autowired
    private TIomWarehouseService tIomWarehouseService;
    @ApiOperation("分页查询库存")
    @GetMapping("/tIoms/{current}/{pageSize}")
    public ResponseEntity<Page<TIomWarehouse>> findPage(@PathVariable("current")Integer current, @PathVariable("pageSize")Integer pageSize){
        Page<TIomWarehouse> tMenuPage = new Page<>(current,pageSize);
        Page<TIomWarehouse> page = tIomWarehouseService.page(tMenuPage);
        return ResponseEntity.ok(tMenuPage);
    }



    @Autowired
    private CargoService cargoService;

    @Autowired
    private WarehouseService warehouseService;

    @ApiOperation("添加移库单详情信息")
    @PostMapping("/iomWarehouse")
    public ResponseEntity<String> add(@RequestBody TIomWarehouse iomWarehouse){
        // 将货位上的商品数量减少
        TCargo cargo = new TCargo();
        if(iomWarehouse.getIomFlag() == 0){
            cargo = cargoService.getOne(new QueryWrapper<TCargo>().lambda().eq(TCargo::getCargoNo,iomWarehouse.getIomCargoloca()));
            cargo.setProductNum(cargo.getProductNum() - iomWarehouse.getIomNumber());
            cargoService.update(cargo,new QueryWrapper<TCargo>().lambda().eq(TCargo::getCargoNo,cargo.getCargoNo()));
        }else {
            TWarehouse warehouse = warehouseService.getOne(new QueryWrapper<TWarehouse>().lambda().eq(TWarehouse::getGoodsNo, iomWarehouse.getIomArea()));
            iomWarehouse.setIomArea(warehouse.getGoodsName());
        }
        // 将移库单详情信息添加到表中
        tIomWarehouseService.save(iomWarehouse);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("通过移库单号查询移库单详情信息")
    @GetMapping("/iomWarehouse/{iomNo}")
    public ResponseEntity<List<TIomWarehouse>> findByNo(@PathVariable("iomNo")String iomNo){
        List<TIomWarehouse> iomWarehouses = tIomWarehouseService.list(new QueryWrapper<TIomWarehouse>().lambda().eq(TIomWarehouse::getIomNo, iomNo));
        return ResponseEntity.ok(iomWarehouses);
    }

    @ApiOperation("通过移库单号和入库状态查询移库单详情信息")
    @GetMapping("/iomWarehouse/{iomFlag}/{iomNo}")
    public ResponseEntity<List<TIomWarehouse>> findByNo(@PathVariable("iomNo")String iomNo,@PathVariable("iomFlag") Integer iomFlag){
        //
        List<TIomWarehouse> iomWarehouses = tIomWarehouseService.list(new QueryWrapper<TIomWarehouse>().lambda().eq(TIomWarehouse::getIomNo, iomNo).eq(TIomWarehouse::getIomFlag, iomFlag));
        return ResponseEntity.ok(iomWarehouses);
    }
}
